home *** CD-ROM | disk | FTP | other *** search
/ 3D Game Programming All in One / 3D Game Programming All in One Disc.iso / 3D2E / RESOURCES / CH5 / EMAGA5 BOOK CODE / control / server / weapons / crossbow.cs next >
Text File  |  2006-09-18  |  13KB  |  433 lines

  1. //============================================================================
  2. // control/players/crossbow.cs
  3. //
  4. // Copyright (c) 2003,2006 Kenneth C. Finney
  5. // Portions Copyright (c) 2001 GarageGames.Com
  6. // Portions Copyright (c) 2001 by Sierra Online, Inc.
  7. //============================================================================
  8.  
  9. //-----------------------------------------------------------------------------
  10. // Crossbow weapon. 
  11. // These objects rely on the item & inventory support system defined
  12. // in item.cs and inventory.cs
  13. //-----------------------------------------------------------------------------
  14.  
  15. //-----------------------------------------------------------------------------
  16. // Crossbow bolt projectile particles
  17.  
  18. datablock ParticleData(CrossbowBoltParticle)
  19. {
  20.    textureName          = "~/data/particles/smoke";
  21.    dragCoeffiecient     = 0.0;
  22.    gravityCoefficient   = -0.2;   // rises slowly
  23.    inheritedVelFactor   = 0.00;
  24.    lifetimeMS           = 500;  // lasts 0.7 second
  25.    lifetimeVarianceMS   = 150;   // ...more or less
  26.    useInvAlpha = false;
  27.    spinRandomMin = -30.0;
  28.    spinRandomMax = 30.0;
  29.  
  30.    colors[0]     = "0.56 0.36 0.26 1.0";
  31.    colors[1]     = "0.56 0.36 0.26 1.0";
  32.    colors[2]     = "0 0 0 0";
  33.  
  34.    sizes[0]      = 0.25;
  35.    sizes[1]      = 0.5;
  36.    sizes[2]      = 1.0;
  37.  
  38.    times[0]      = 0.0;
  39.    times[1]      = 0.3;
  40.    times[2]      = 1.0;
  41. };
  42.  
  43. datablock ParticleEmitterData(CrossbowBoltEmitter)
  44. {
  45.    ejectionPeriodMS = 10;
  46.    periodVarianceMS = 5;
  47.  
  48.    ejectionVelocity = 0.25;
  49.    velocityVariance = 0.10;
  50.  
  51.    thetaMin         = 0.0;
  52.    thetaMax         = 90.0;
  53.  
  54.    particles = CrossbowBoltParticle;
  55. };
  56.  
  57.  
  58. //-----------------------------------------------------------------------------
  59. // Projectile Explosion
  60.  
  61. datablock ParticleData(CrossbowExplosionParticle)
  62. {
  63.    textureName          = "~/data/particles/smoke";
  64.    dragCoefficient      = 2;
  65.    gravityCoefficient   = 0.2;
  66.    inheritedVelFactor   = 0.2;
  67.    constantAcceleration = 0.0;
  68.    lifetimeMS           = 1000;
  69.    lifetimeVarianceMS   = 150;
  70.  
  71.    colors[0]     = "0.56 0.36 0.26 1.0";
  72.    colors[1]     = "0.56 0.36 0.26 0.0";
  73.  
  74.    sizes[0]      = 0.5;
  75.    sizes[1]      = 1.0;
  76. };
  77.  
  78. datablock ParticleEmitterData(CrossbowExplosionEmitter)
  79. {
  80.    ejectionPeriodMS = 7;
  81.    periodVarianceMS = 0;
  82.    ejectionVelocity = 2;
  83.    velocityVariance = 1.0;
  84.    ejectionOffset   = 0.0;
  85.    thetaMin         = 0;
  86.    thetaMax         = 60;
  87.    phiReferenceVel  = 0;
  88.    phiVariance      = 360;
  89.    overrideAdvances = false;
  90.    particles = "CrossbowExplosionParticle";
  91. };
  92.  
  93. datablock ParticleData(CrossbowExplosionSmoke)
  94. {
  95.    textureName          = "~/data/particles/smoke";
  96.    dragCoeffiecient     = 100.0;
  97.    gravityCoefficient   = 0;
  98.    inheritedVelFactor   = 0.25;
  99.    constantAcceleration = -0.80;
  100.    lifetimeMS           = 1200;
  101.    lifetimeVarianceMS   = 300;
  102.    useInvAlpha =  true;
  103.    spinRandomMin = -80.0;
  104.    spinRandomMax =  80.0;
  105.  
  106.    colors[0]     = "0.56 0.36 0.26 1.0";
  107.    colors[1]     = "0.2 0.2 0.2 1.0";
  108.    colors[2]     = "0.0 0.0 0.0 0.0";
  109.  
  110.    sizes[0]      = 1.0;
  111.    sizes[1]      = 1.5;
  112.    sizes[2]      = 2.0;
  113.  
  114.    times[0]      = 0.0;
  115.    times[1]      = 0.5;
  116.    times[2]      = 1.0;
  117.  
  118. };
  119.  
  120. datablock ParticleEmitterData(CrossbowExplosionSmokeEmitter)
  121. {
  122.    ejectionPeriodMS = 10;
  123.    periodVarianceMS = 0;
  124.    ejectionVelocity = 4;
  125.    velocityVariance = 0.5;
  126.    thetaMin         = 0.0;
  127.    thetaMax         = 180.0;
  128.    lifetimeMS       = 250;
  129.    particles = "CrossbowExplosionSmoke";
  130. };
  131.  
  132. datablock ParticleData(CrossbowExplosionSparks)
  133. {
  134.    textureName          = "~/data/particles/spark";
  135.    dragCoefficient      = 1;
  136.    gravityCoefficient   = 0.0;
  137.    inheritedVelFactor   = 0.2;
  138.    constantAcceleration = 0.0;
  139.    lifetimeMS           = 500;
  140.    lifetimeVarianceMS   = 350;
  141.  
  142.    colors[0]     = "0.60 0.40 0.30 1.0";
  143.    colors[1]     = "0.60 0.40 0.30 1.0";
  144.    colors[2]     = "1.0 0.40 0.30 0.0";
  145.  
  146.    sizes[0]      = 0.5;
  147.    sizes[1]      = 0.25;
  148.    sizes[2]      = 0.25;
  149.  
  150.    times[0]      = 0.0;
  151.    times[1]      = 0.5;
  152.    times[2]      = 1.0;
  153. };
  154.  
  155. datablock ParticleEmitterData(CrossbowExplosionSparkEmitter)
  156. {
  157.    ejectionPeriodMS = 3;
  158.    periodVarianceMS = 0;
  159.    ejectionVelocity = 13;
  160.    velocityVariance = 6.75;
  161.    ejectionOffset   = 0.0;
  162.    thetaMin         = 0;
  163.    thetaMax         = 180;
  164.    phiReferenceVel  = 0;
  165.    phiVariance      = 360;
  166.    overrideAdvances = false;
  167.    orientParticles  = true;
  168.    lifetimeMS       = 100;
  169.    particles = "CrossbowExplosionSparks";
  170. };
  171.  
  172. datablock ExplosionData(CrossbowSubExplosion1)
  173. {
  174.    offset = 1.0;
  175.    emitter[0] = CrossbowExplosionSmokeEmitter;
  176.    emitter[1] = CrossbowExplosionSparkEmitter;
  177. };
  178.  
  179. datablock ExplosionData(CrossbowSubExplosion2)
  180. {
  181.    offset = 1.0;
  182.    emitter[0] = CrossbowExplosionSmokeEmitter;
  183.    emitter[1] = CrossbowExplosionSparkEmitter;
  184. };
  185.  
  186. datablock ExplosionData(CrossbowExplosion)
  187. {
  188.   // soundProfile   = CrossbowExplosionSound;
  189.    lifeTimeMS = 1200;
  190.  
  191.    // Volume particles
  192.    particleEmitter = CrossbowExplosionEmitter;
  193.    particleDensity = 80;
  194.    particleRadius = 1;
  195.  
  196.    // Point emission
  197.    emitter[0] = CrossbowExplosionSmokeEmitter;
  198.    emitter[1] = CrossbowExplosionSparkEmitter;
  199.  
  200.    // Sub explosion objects
  201.    subExplosion[0] = CrossbowSubExplosion1;
  202.    subExplosion[1] = CrossbowSubExplosion2;
  203.  
  204.    // Camera Shaking
  205.    shakeCamera = true;
  206.    camShakeFreq = "10.0 11.0 10.0";
  207.    camShakeAmp = "1.0 1.0 1.0";
  208.    camShakeDuration = 0.5;
  209.    camShakeRadius = 10.0;
  210.  
  211.    // Dynamic light
  212.    lightStartRadius = 6;
  213.    lightEndRadius = 3;
  214.    lightStartColor = "0.5 0.5 0";
  215.    lightEndColor = "0 0 0";
  216. };
  217.  
  218.  
  219. //-----------------------------------------------------------------------------
  220. // Projectile Object
  221.  
  222. datablock ProjectileData(CrossbowProjectile)
  223. {
  224.    projectileShapeName = "~/data/models/weapons/bolt.dts";
  225.    directDamage        = 20;
  226.    radiusDamage        = 20;
  227.    damageRadius        = 1.5;
  228.    explosion           = CrossbowExplosion;
  229.    particleEmitter     = CrossbowBoltEmitter;
  230.  
  231.    muzzleVelocity      = 100;
  232.    velInheritFactor    = 0.3;
  233.  
  234.    armingDelay         = 0;
  235.    lifetime            = 5000;
  236.    fadeDelay           = 5000;
  237.    bounceElasticity    = 0;
  238.    bounceFriction      = 0;
  239.    isBallistic         = true;
  240.    gravityMod = 0.80;
  241.  
  242.    hasLight    = true;
  243.    lightRadius = 4.0;
  244.    lightColor  = "0.5 0.5 0";
  245. };
  246.  
  247. function CrossbowProjectile::onCollision(%this,%obj,%col,%fade,%pos,%normal)
  248. {
  249.    // Apply damage to the object all shape base objects
  250.    if (%col.getType() & $TypeMasks::ShapeBaseObjectType)
  251.       %col.damage(%obj,%pos,%this.directDamage,"CrossbowBolt");
  252.  
  253.    // Radius damage is a support scripts defined in radiusDamage.cs
  254.    radiusDamage(%obj,%pos,%this.damageRadius,%this.radiusDamage,"CrossbowBolt",0);
  255. }
  256.  
  257.  
  258. //-----------------------------------------------------------------------------
  259. // Ammo Item
  260.  
  261. datablock ItemData(CrossbowAmmo)
  262. {
  263.    // Mission editor category
  264.    category = "Ammo";
  265.  
  266.    // Add the Ammo namespace as a parent.  The ammo namespace provides
  267.    // common ammo related functions and hooks into the inventory system.
  268.    className = "Ammo";
  269.  
  270.    // Basic Item properties
  271.    shapeFile = "~/data/models/weapons/boltclip.dts";
  272.    mass = 1;
  273.    elasticity = 0.2;
  274.    friction = 0.6;
  275.  
  276.     // Dynamic properties defined by the scripts
  277.     pickUpName = "crossbow bolts";
  278.    maxInventory = 20;
  279. };
  280.  
  281.  
  282. //--------------------------------------------------------------------------
  283. // Weapon Item.  This is the item that exists in the world, i.e. when it's
  284. // been dropped, thrown or is acting as re-spawnable item.  When the weapon
  285. // is mounted onto a shape, the CrossbowImage is used.
  286.  
  287. datablock ItemData(Crossbow)
  288. {
  289.    // Mission editor category
  290.    category = "Weapon";
  291.  
  292.    // Hook into Item Weapon class hierarchy. The weapon namespace
  293.    // provides common weapon handling functions in addition to hooks
  294.    // into the inventory system.
  295.    className = "Weapon";
  296.  
  297.    // Basic Item properties
  298.    shapeFile = "~/data/models/weapons/crossbow.dts";
  299.    mass = 1;
  300.    elasticity = 0.2;
  301.    friction = 0.6;
  302.    emap = true;
  303.  
  304.     // Dynamic properties defined by the scripts
  305.     pickUpName = "a crossbow";
  306.     image = CrossbowImage;
  307. };
  308.  
  309.  
  310. //--------------------------------------------------------------------------
  311. // Crossbow image which does all the work.  Images do not normally exist in
  312. // the world, they can only be mounted on ShapeBase objects.
  313.  
  314. datablock ShapeBaseImageData(CrossbowImage)
  315. {
  316.    // Basic Item properties
  317.    shapeFile = "~/data/models/weapons/crossbow.dts";
  318.    emap = true;
  319.  
  320.    // Specify mount point & offset for 3rd person, and eye offset
  321.    // for first person rendering.
  322.    mountPoint = 0;
  323.    eyeOffset = "0.1 0.4 -0.6";
  324.  
  325.    // When firing from a point offset from the eye, muzzle correction
  326.    // will adjust the muzzle vector to point to the eye LOS point.
  327.    // Since this weapon doesn't actually fire from the muzzle point,
  328.    // we need to turn this off.
  329.    correctMuzzleVector = false;
  330.  
  331.    // Add the WeaponImage namespace as a parent, WeaponImage namespace
  332.    // provides some hooks into the inventory system.
  333.    className = "WeaponImage";
  334.  
  335.    // Projectile && Ammo.
  336.    item = Crossbow;
  337.    ammo = CrossbowAmmo;
  338.    projectile = CrossbowProjectile;
  339.    projectileType = Projectile;
  340.  
  341.    // Images have a state system which controls how the animations
  342.    // are run, which sounds are played, script callbacks, etc. This
  343.    // state system is downloaded to the client so that clients can
  344.    // predict state changes and animate accordingly.  The following
  345.    // system supports basic ready->fire->reload transitions as
  346.    // well as a no-ammo->dryfire idle state.
  347.  
  348.    // Initial start up state
  349.    stateName[0]                     = "Preactivate";
  350.    stateTransitionOnLoaded[0]       = "Activate";
  351.    stateTransitionOnNoAmmo[0]       = "NoAmmo";
  352.  
  353.    // Activating the gun.  Called when the weapon is first
  354.    // mounted and there is ammo.
  355.    stateName[1]                     = "Activate";
  356.    stateTransitionOnTimeout[1]      = "Ready";
  357.    stateTimeoutValue[1]             = 0.6;
  358.    stateSequence[1]                 = "Activate";
  359.  
  360.    // Ready to fire, just waiting for the trigger
  361.    stateName[2]                     = "Ready";
  362.    stateTransitionOnNoAmmo[2]       = "NoAmmo";
  363.    stateTransitionOnTriggerDown[2]  = "Fire";
  364.  
  365.    // Fire the weapon. Calls the fire script which does
  366.    // the actual work.
  367.    stateName[3]                     = "Fire";
  368.    stateTransitionOnTimeout[3]      = "Reload";
  369.    stateTimeoutValue[3]             = 0.2;
  370.    stateFire[3]                     = true;
  371.    stateRecoil[3]                   = LightRecoil;
  372.    stateAllowImageChange[3]         = false;
  373.    stateSequence[3]                 = "Fire";
  374.    stateScript[3]                   = "onFire";
  375.   // stateSound[3]                    = CrossbowFireSound;
  376.  
  377.    // Play the relead animation, and transition into
  378.    stateName[4]                     = "Reload";
  379.    stateTransitionOnNoAmmo[4]       = "NoAmmo";
  380.    stateTransitionOnTimeout[4]      = "Ready";
  381.    stateTimeoutValue[4]             = 0.8;
  382.    stateAllowImageChange[4]         = false;
  383.    stateSequence[4]                 = "Reload";
  384.    stateEjectShell[4]               = true;
  385.  //  stateSound[4]                    = CrossbowReloadSound;
  386.  
  387.    // No ammo in the weapon, just idle until something
  388.    // shows up. Play the dry fire sound if the trigger is
  389.    // pulled.
  390.    stateName[5]                     = "NoAmmo";
  391.    stateTransitionOnAmmo[5]         = "Reload";
  392.    stateSequence[5]                 = "NoAmmo";
  393.    stateTransitionOnTriggerDown[5]  = "DryFire";
  394.  
  395.    // No ammo dry fire
  396.    stateName[6]                     = "DryFire";
  397.    stateTimeoutValue[6]             = 1.0;
  398.    stateTransitionOnTimeout[6]      = "NoAmmo";
  399.  //  stateSound[6]                    = CrossbowFireEmptySound;
  400. };
  401.  
  402.  
  403. //-----------------------------------------------------------------------------
  404.  
  405. function CrossbowImage::onFire(%this, %obj, %slot)
  406. {
  407.    %projectile = %this.projectile;
  408.  
  409.    // Decrement inventory ammo. The image's ammo state is update
  410.    // automatically by the ammo inventory hooks.
  411.    %obj.decInventory(%this.ammo,1);
  412.  
  413.    // Determin initial projectile velocity based on the
  414.    // gun's muzzle point and the object's current velocity
  415.    %muzzleVector = %obj.getMuzzleVector(%slot);
  416.    %objectVelocity = %obj.getVelocity();
  417.    %muzzleVelocity = VectorAdd(
  418.       VectorScale(%muzzleVector, %projectile.muzzleVelocity),
  419.       VectorScale(%objectVelocity, %projectile.velInheritFactor));
  420.  
  421.    // Create the projectile object
  422.    %p = new (%this.projectileType)() {
  423.       dataBlock        = %projectile;
  424.       initialVelocity  = %muzzleVelocity;
  425.       initialPosition  = %obj.getMuzzlePoint(%slot);
  426.       sourceObject     = %obj;
  427.       sourceSlot       = %slot;
  428.       client           = %obj.client;
  429.    };
  430.    MissionCleanup.add(%p);
  431.    return %p;
  432. }
  433.